﻿using FoftmAging.Core;
using Ice.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FoftmAging
{
    public partial class ConfigForm : Form
    {
        private Config _config;

        private List<string> _portNames;
        public ConfigForm()
        {
            InitializeComponent();
            _config = IniHelper.GetConfig();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            txtMilliSecond.Text = _config.IntervalMillisecond.ToString();
            comboBox1.Text = _config.BaudRate.ToString();
            cboGroupCount.SelectedIndex = _config.GroupCount - 1;
            txtMin.Text = _config.MinTemperature.ToString();
            txtMax.Text = _config.MaxTemperature.ToString();
            txtStartupTime.Text = _config.StartupTime.ToString();

            _portNames = SerialPort.GetPortNames().ToList();


            chbIsResetChannel.Checked = _config.IsResetChannel;

            var list = _config.GetGroupSettings();

            foreach (var item in list)
            {
                if (!_portNames.Contains(item.PortName)) item.PortName = null;
                var i = dataGridView1.Rows.Add(item.GroupNo, item.PortName, item.ChannelCount);
                var combox = dataGridView1.Rows[i].Cells[1] as DataGridViewComboBoxCell;
                combox.Items.AddRange(_portNames.ToArray());
            }
            dataGridView1.CurrentCell = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _config.IntervalMillisecond = txtMilliSecond.Text.ToInt32();
            _config.BaudRate = comboBox1.Text.ToInt32();
            _config.GroupCount = cboGroupCount.SelectedIndex + 1;
            _config.MinTemperature = txtMin.Text.ToInt32();
            _config.MaxTemperature = txtMax.Text.ToInt32();
            _config.StartupTime = txtStartupTime.Text.ToInt32();

            var list = new List<GroupSetting>();
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                list.Add(new GroupSetting
                {
                    GroupNo = dataGridView1[0, i].Value.ToString().ToInt32(),
                    PortName = dataGridView1[1, i].Value?.ToString() ?? "",
                    ChannelCount = dataGridView1[2, i].Value.ToString().Trim().ToInt32(),
                });
            }

            foreach (var item in list.Take(_config.GroupCount))
            {
                if (string.IsNullOrEmpty(item.PortName))
                {
                    MessageBox.Show($"组{item.GroupNo} 串口号不能为空");
                    return;
                }
            }

            _config.IsResetChannel = chbIsResetChannel.Checked;

            _config.SetGroupSettings(list);
            _config.SaveConfig();
            DialogResult = DialogResult.OK;
        }


    }
}
